package com.rapida.app;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import java.net.PortUnreachableException;

/**
 * Created by mm on 15-1-16.
 */
public class SoundClips {
    private static int[] SOUND_RES;
    private static SoundPoolPlayer mPlayer;

    public interface Player {
        public void release();

        public void play(int action);
    }

    public static void initSoundRes(int[] res) {
        SOUND_RES = res;
    }

    public static Player getPlayer(Context context) {
        if (SOUND_RES == null) {
            throw new RuntimeException("first invoke initSoundRes()");
        }
        if (mPlayer == null) {
            mPlayer = new SoundPoolPlayer(context);
        }
        return mPlayer;
    }

    public static class SoundPoolPlayer implements Player, SoundPool.OnLoadCompleteListener {
        private static final int ID_NOT_LOADED = 0;
        private SoundPool mSoundPool;
        private static final int NUM_SOUND_STREAMS = 1;
        private final int[] mSoundRes = {0, 1, 1};
        private final int[] mSoundIDs;
        private final boolean[] mSoundIDReady;
        private int mSoundIDToPlay;
        private Context mContext;


        public SoundPoolPlayer(Context context) {
            mContext = context;
            mSoundIDToPlay = ID_NOT_LOADED;

            mSoundPool = new SoundPool(NUM_SOUND_STREAMS, AudioManager.STREAM_RING, 0);
            mSoundPool.setOnLoadCompleteListener(this);

            mSoundIDs = new int[SOUND_RES.length];
            mSoundIDReady = new boolean[SOUND_RES.length];
            for (int i = 0; i < SOUND_RES.length; i++) {
                mSoundIDs[i] = mSoundPool.load(mContext, SOUND_RES[i], 1);
                mSoundIDReady[i] = false;
            }
        }

        @Override
        public void onLoadComplete(SoundPool pool, int soundID, int status) {
            if (status != 0) {
                for (int i = 0; i < mSoundIDs.length; i++) {
                    if (mSoundIDs[i] == soundID) {
                        mSoundIDs[i] = ID_NOT_LOADED;
                        break;
                    }
                }
                return;
            }

            for (int i = 0; i < mSoundIDs.length; i++) {
                if (mSoundIDs[i] == soundID) {
                    mSoundIDReady[i] = true;
                    break;
                }
            }

            if (soundID == mSoundIDToPlay) {
                mSoundIDToPlay = ID_NOT_LOADED;
                mSoundPool.play(soundID, 1f, 1f, 0, 0, 1f);
            }
        }

        @Override
        public void release() {
            if (mSoundPool != null) {
                mSoundPool.release();
                mSoundPool = null;
            }
        }

        @Override
        public void play(int action) {
            int index = mSoundRes[action];
            if (mSoundIDs[index] == ID_NOT_LOADED) {
                mSoundIDs[index] = mSoundPool.load(mContext, SOUND_RES[index], 1);
                mSoundIDToPlay = mSoundIDs[index];
            } else if (!mSoundIDReady[index]) {
                mSoundIDToPlay = mSoundIDs[index];
            } else {
                mSoundPool.play(mSoundIDs[index], 1f, 1f, 0, 0, 1f);
            }

        }
    }

}
